import * as express from "express";
import * as path from "path";
import {User} from '../models/User';
import {BaseController} from './BaseController';
import {AddDocDto} from '../dtos/docs/AddDocDto';
import {UpdateDocDto} from '../dtos/docs/UpdateDocDto';
import {DocsService} from '../services/DocsService';
import {GroupsService} from '../services/GroupsService';
import {ProjectsService} from '../services/ProjectsService';
import {DocsWithProjectDto} from '../dtos/docs/return/DocsWithProjectDto';
import {Doc} from '../models/Doc';
export class DocController extends BaseController
{
  docsService:DocsService;
  groupsService:GroupsService;
  projectsService:ProjectsService;
  constructor(app:express.Application){
    super(app);
    var $this = this;
    app.post(this.domain+"/docs",(req,res)=>{
      $this.handerErr(req,res,(user:User)=>{
        $this.addDoc(req,res);
      },true);

    });
    app.get(this.domain+"/docs/:id",(req,res)=>{
      $this.handerErr(req,res,()=>{
        $this.getDoc(req,res);
      },true);

    });
    app.put(this.domain+"/docs/:id",(req,res)=>{
      $this.handerErr(req,res,()=>{
        $this.updateDoc(req,res);
      },true);

    });
    app.delete(this.domain+"/docs/:id",(req,res)=>{
      $this.handerErr(req,res,()=>{
        $this.deleteDoc(req,res);
      },true);

    });
    app.get(this.domain+"/docs/project/:id",(req,res)=>{
      $this.handerErr(req,res,()=>{
        $this.getDocByProjectId(req,res);
      },true);

    });
    app.get(this.domain+"/docs/group/:id",(req,res)=>{
      $this.handerErr(req,res,()=>{
        $this.getDocByGroupId(req,res);
      },true);

    });
    this.docsService = new DocsService();
    this.groupsService = new GroupsService();
    this.projectsService = new ProjectsService();
  }

  addDoc(req: express.Request, res: express.Response)
  {
    this.docsService.addDoc(AddDocDto.toDoc(req.body),(err,doc:Doc)=>{
      res.send(JSON.stringify(doc));
    });
  }

  getDoc(req: express.Request, res: express.Response)
  {
    var docId:string = req.params.id;
    this.docsService.getDoc(docId,(err,doc)=>{
      res.send(JSON.stringify(doc));
    })
  }

  getDocByGroupId(req: express.Request, res: express.Response)
  {
    super.initResponse(res);
    var groupID:string = req.params.id;
    this.docsService.getDocsByGroupId(groupID,(err,docs)=>{
      res.send(JSON.stringify(docs));
    })
  }

  getDocByProjectId(req: express.Request, res: express.Response)
  {
    super.initResponse(res);
    var projectId:string = req.params.id;
    this.projectsService.getProject(projectId,(err,project)=>{
      this.docsService.getDocsByProjectId(projectId,(err,docs)=>{
        this.groupsService.getGroups(projectId,(err,groups)=>{
          res.send(JSON.stringify(DocsWithProjectDto.toReturn(project,groups,docs)));
        })
      })
    })

  }

  updateDoc(req: express.Request, res: express.Response)
  {
    var docId:string = req.params.id;
    this.docsService.udpateDoc(UpdateDocDto.toDoc(docId,req.body),(err,doc:Doc)=>{
      res.send(JSON.stringify(doc));
    });
  }

  deleteDoc(req: express.Request, res: express.Response)
  {
    var docId:string = req.params.id;
    this.docsService.deleteDoc(docId,(err,doc:Doc)=>{
      res.send(JSON.stringify(doc));
    });
  }
}
